<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 
            层序遍历获取每一层，然后计算每一层的平均值
            */
      var averageOfLevels = function (root) {
        let result = []
        function levelOrder(root) {
          let res = []
          let queue = [root]
          while (queue.length) {
            res.push([])
            let len = queue.length
            for (let i = 0; o < len; i++) {
              let node = queue.shift()
              res[res.length - 1].push(node.val)
              if (node.left) {
                queue.push(node.left)
              }
              if (node.right) {
                queue.push(node.right)
              }
            }
          }
          return res
        }
        let res = levelOrder(root)
        result = res.map(item => {
          return item.reduce((pre, i) => (pre += item), 0) / item.length
        })
        console.log(result)
      }
    </script>
  </body>
</html>
